﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using HRX.PayCenter.ApplicationCore.Models;
using HRX.PayCenter.ApplicationCore.Interfaces;
using Microsoft.Extensions.Logging;
using HRX.PayCenter.Contract;
using HRX.PayCenter.Web.Filters;
using HRX.PayCenter.ApplicationCore.Exceptions;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HRX.PayCenter.Web.Controllers.Api
{
    /// <summary>
    /// 支付
    /// </summary>
    [Route("api/[controller]")]
    public class PayController : Controller
    {
        private readonly IRouteService _routeService;
        private readonly IAppLogger<PayController> _appLogger;

        /// <summary>
        /// 
        /// </summary>
        public PayController(IRouteService routeService, IAppLogger<PayController> appLogger)
        {
            _routeService = routeService;
            _appLogger = appLogger;
        }

        /// <summary>
        /// 支付网关
        /// </summary>
        /// <returns></returns>
        [HttpPost("gateway")]
        [SignValidate(IsUse = true)]
        public async Task<ResultSet<string>> Gateway([FromBody] TradeRequest request)
        {
            var payService = _routeService.GetPayService(request.Method); //路由地址
            var result = await payService.GetWapTradeContentWithOrder(request);
            return ResultSet<string>.Success(result);
        }

        /// <summary>
        /// 退款
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("refund")]
        [SignValidate(IsUse = true)]
        public async Task<ResultSet<bool>> Refund([FromBody] RefundRequest request)
        {
            var payService = _routeService.GetPayServiceByOrderNo(request.OrderNo);
            var result = await payService.Refund(request);
            return ResultSet<bool>.YesOrNo(result, "退款失败");
        }

        /// <summary>
        /// 分账
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("divise")]
        [SignValidate(IsUse = true)]
        public async Task<ResultSet<bool>> Divise([FromBody] DiviseRequet request)
        {
            var payService = _routeService.GetPayServiceByOrderNo(request.OrderNo);
            var result = await payService.Transfer(request);
            return ResultSet<bool>.YesOrNo(result, "分账失败");
        }
    }
}
